<template>
  <div>
    <!-- close :所有的、任意形式的-->
    <el-dialog
      :title="isAdd ? '添加管理员' : '修改管理员'"
      :visible.sync="info.isShow"
      width="30%"
      @close="cancel"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
         <el-form-item label="角色名称" prop="roleid">
        <el-select v-model="ruleForm.roleid" placeholder="请选择角色">
         
          <el-option
            v-for="(item, index) in roleList"
            :key="index"
            :label="item.rolename"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
        <el-form-item label="管理员名称" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="管理员密码" prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
      
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="ruleForm.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 导入负责添加角色的函数
import { userAdd, userEdit } from "../../request";
let defaultData = {
  rolename: "",
  menus: [],
  status: 1,
};
export default {
  props: {
    info: {
      type: Object,
      default: {
        isAdd: false,
        isShow: false,
      },
    },
  },
  data() {
    return {
      dialogVisible: false,
        // 下拉选框的数据
      roleList: [],
      // 表单的数据
      ruleForm: {
        roleid:1,username:'',password:'',status:1
      },
      // 表单的规则
      rules: {
        roleid: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
        ],
        username: [
          { required: true, message: "请输入管理员名称", trigger: "blur" },
        ],
      },
      // 树形控件的角色权限
      roleData: [],
      defaultProps: {
        // 子节点
        children: "children",
        // 文字内容
        label: "title",
      },
      // 弹窗类型，添加还是修改
      isAdd: true,
    };
  },

  created() {
    // 获取下拉列表的数据
    this.getRoleList();
  },
  // mounted() {
  //   // 设置树形控件中被选中的节点
  //   this.setKeys();
  // },
  methods: {
    getRoleList() {
      //gat请求带参数的方式
      //  另一种方式get("/menulist?istree=1")
      this.$http.get("/rolelist").then((res) => {
        // 获取完整的权限列表，用于分配权限
        this.roleList = res.list;
      });
    },

    // 设置当前组件状态
    setStatus(val) {
      // this.dialogVisible = val.isShow;
      // this.isAdd = val.isAdd
      this.ruleForm = { ...val };
    },
    // 提交表单
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          // 封装请求
          let res;
          if (this.info.isAdd) {
            res = userAdd(this.ruleForm);
          } else {
            res = userEdit(this.ruleForm);
          }
          if (res) {
            // 重置弹窗数据
            this.cancel();
            // 关闭弹窗
            this.info.isShow = false;
            // 兄弟组件传值 告诉list组件更新数据
            this.$bus.$emit("update");
          }
        } else {
          return false;
        }
      });
    },
    cancel() {
      this.ruleForm = { ...defaultData };
      // 关闭弹窗
      this.dialogVisible = false;
          // 关闭弹窗
      this.info.isShow=false
    },
  },
};
</script>

<style>
</style>